﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class SkillManager
{
    private Dictionary<string, SkillRuntimeInfo> skillRuntimeInfoDic = new Dictionary<string, SkillRuntimeInfo>();

    private Animator animator;
    /// <summary>
    /// 正在运行的技能
    /// </summary>
    public List<SkillRuntimeInfo> currentRunningSkills = new List<SkillRuntimeInfo>();
    /// <summary>
    /// 正在CD的技能
    /// </summary>
    public List<SkillRuntimeInfo> currentCDSkills = new List<SkillRuntimeInfo>();
    public SkillManager(Animator animator)
    {
        this.animator = animator;
    }

    public void Update()
    {
        RunCDLogic();
    }

    private List<SkillRuntimeInfo> cdRemoveList = new List<SkillRuntimeInfo>();
    private void RunCDLogic()
    {
        for (int i = 0; i < currentCDSkills.Count; i++)
        {
            SkillRuntimeInfo s = currentCDSkills[i];
            s.CD_Time -= Time.unscaledDeltaTime;

            if (s.CD_Time <= 0)
                cdRemoveList.Add(s);
        }

        for (int i = 0; i < cdRemoveList.Count; i++)
        {
            currentCDSkills.Remove(cdRemoveList[i]);
        }
        cdRemoveList.Clear();
    }

    public SkillRuntimeInfo GetSkillRuntimeInfo(string skillName)
    {
        if (skillRuntimeInfoDic.ContainsKey(skillName))
            return skillRuntimeInfoDic[skillName];

        return null;
    }
    public void AddSkill(SkillDefineInfo info)
    {
        if (!skillRuntimeInfoDic.ContainsKey(info.name))
        {
            SkillRuntimeInfo r = new SkillRuntimeInfo();
            r.defineInfo = info;
            skillRuntimeInfoDic.Add(info.name, r);
        }
    }
    public void RemoveSkill(string skillName)
    {
        if (skillRuntimeInfoDic.ContainsKey(skillName))
        {
            skillRuntimeInfoDic.Remove(skillName);
        }
    }

  
    public bool StartRunSkill(string skillName)
    {
        if (!skillRuntimeInfoDic.ContainsKey(skillName))
            return false;

        SkillRuntimeInfo info = skillRuntimeInfoDic[skillName];
        if (!CheckSkillCanStart(info))
            return false;

        info.skillRunState = SkillRunState.StartRunning;
        currentRunningSkills.Add(info);

        return true;
    }

    public bool CheckSkillCanStart(SkillRuntimeInfo info)
    {
        if (info.CD_Time > 0)
            return false;
        if (info.skillRunState == SkillRunState.StartRunning)
            return false;


        return false;
    }
   
}

